// pages/account/programme/programme.js
const app = getApp()
const { wxPost, wxGet, commonShare, serviceBusy } = require('../../../utils/common.js')

Page({


  data: {
    adjustId:''
  },
  onLoad: function (options) {
    const { adjustId } = options
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 3] //上一个页面
    if (prevPage) {
      this.setData({
        infoId: options.infoId || '',
        adjustId,
        isGuest: !!prevPage.data.isGuest
      })
    } else {
      this.setData({ infoId: options.infoId || '',adjustId})
    }
    
    this.loadAdjustInfo(adjustId)
  },
  loadAdjustInfo(adjustId){
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    const{infoId}=this.data
    wxGet('/assetsInfo/adjustInfo',{infoId,adjustId},({data})=>{
      if (data && data['errorCode'] === 0) {
        const { infoId, adjustId, list } = data
        this.buyList = list
        let buyList = [], sellList = [], sellNoneNetValueList = []
        list && list.forEach(o => {
          if (o.dealType === 0) {
            buyList.push(o)
          } else {
            if (o.fundId.fundType === '净值型') {
              sellList.push((o))
            } else {
              sellNoneNetValueList.push((o))
            }
          }
        })
        let name = `${(infoId && (infoId.supplyName || infoId.nickName))} - ${adjustId.adjustDate} 投资方案`

        this.setData({ list, infoId, adjustId, buyList, sellList, sellNoneNetValueList, name, status: adjustId && adjustId.status})
      }
    },null,()=>{
      wx.hideLoading()
    })
  },
  onShareAppMessage: function () {
    return commonShare()
  },
  confirm:function(){
    const that=this
    var pages = getCurrentPages()
    var mineHomePage = pages[pages.length - 3] //上一个页面 我的
    const { infoId, adjustId } = that.data
    if (mineHomePage.data.isGuest) {
      wx.showToast({
        title: '测试数据,不能确认!',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '确认中,请稍候...',
      mask:true
    })
    wxPost('/assetsInfo/adjustConfirm', { infoId, adjustId},({data})=>{
      wx.hideLoading()
      if (data && data['erroCode'] === 0) {
        that.setData({ status:1})
        var prevPage = pages[pages.length - 2] //上一个页面
        if (prevPage) {
          prevPage.loadList()
        }
        if (mineHomePage) {
          mineHomePage.loadAssetsInfo()
        }
        wx.showToast({
          title: '确认成功'
        })
      } else {
        wx.showToast({
          title: data['msg'] || '操作失败',
          icon: 'none'
        })
      }
    },()=>{
      wx.hideLoading()
    })
  }
})